package com.atguigu.tingshu.account.receive;

import com.atguigu.tingshu.account.service.UserAccountService;
import com.atguigu.tingshu.common.rabbit.constant.MqConst;
import lombok.SneakyThrows;
import org.apache.commons.lang3.StringUtils;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.core.Message;
@Component
public class UserAccountInListener {
    @Autowired
    UserAccountService userAccountService;

    @RabbitListener(bindings = @QueueBinding(value = @Queue(name = MqConst.QUEUE_ACCOUNT_INIT, durable = "true"),
    exchange = @Exchange(value = MqConst.EXCHANGE_ACCOUNT_INIT, durable = "true"),
    key = MqConst.ROUTING_ACCOUNT_INIT))
    @SneakyThrows
    public void initUserAccount(String userId, Channel channel, Message message){
        // 1.判断消息
        if (StringUtils.isEmpty(userId)) {
            return;
        }
        // 2. 消费消息
        userAccountService.initUserAccount(userId);
        channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);

    }
}
